package com.bycx.system.service.impl;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bycx.core.constant.PubBusinessConstant;
import com.bycx.core.service.BaseService;
import com.bycx.core.util.BeanUtil;
import com.bycx.core.util.StringUtils;
import com.bycx.system.mapper.ASysCodeInfoMapper;
import com.bycx.system.model.ASysCodeInfo;
import com.bycx.system.service.IASysCodeInfoService;

@Service
public class ASysCodeInfoServiceImpl  implements IASysCodeInfoService{
	
	private static final String DEFUALT_GROUP_KEY = "__default__";
	private static final String LEVEL_CHILDREN_KEY = "children";
	
	private static final String TYPE_CODE_KEY = "typeCode";
	private static final String LEVEL_KEY = "level"; //层级
	private static final String TYPE_CODE_STATUS = "status";
	
	@Autowired
	private ASysCodeInfoMapper aSysCodeInfoMapper;

	@Override
	public ASysCodeInfo get(Map<String,Object> map){
		return aSysCodeInfoMapper.get(map);
	}
	@Override
	public List<ASysCodeInfo> getList(Map<String,Object> map){
		return aSysCodeInfoMapper.getList(map);
	}

	
	@Override
	public Map<String, Object> getCode(String typeCode) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("typeCode", typeCode);
		List<ASysCodeInfo> codeList=aSysCodeInfoMapper.getList(map);//__deault__
		List<String> groupCodes=aSysCodeInfoMapper.getGroupCodes(map);
		
		Map<String,Object> re=new HashMap<String,Object>();
		
		Map<String,Object> type=new HashMap<String,Object>();
		type.put("__default__", codeList);
		
		for(String groupCode:groupCodes){
			type.put(groupCode, aSysCodeInfoMapper.getCodesByGroupCode(groupCode));
			
		}
		re.put(typeCode, type);
		return re;
	}
	@Override
	public Map<String, Object> getCode(List<String> list) {
		Map<String,Object> re=new HashMap<String,Object>();
		for(String typeCode:list){
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("typeCode", typeCode);
			List<ASysCodeInfo> codeList=aSysCodeInfoMapper.getList(map);//__deault__
			List<String> groupCodes=aSysCodeInfoMapper.getGroupCodes(map);
			
			Map<String,Object> type=new HashMap<String,Object>();
			type.put("__default__", codeList);
			for(String groupCode:groupCodes){
				type.put(groupCode, aSysCodeInfoMapper.getCodesByGroupCode(groupCode));
			}
			re.put(typeCode, type);
		}
		return re;
	}
	
	
	/**
	 * 获取级联的码表独享
	 * @param aSysCodeInfos
	 * @param level 层级
	 * @return
	 */
	@SuppressWarnings("unused")
	private List<?> getLevelCode( List<ASysCodeInfo> aSysCodeInfos, Integer level ) {
		if( level == null || level <= 0 ) {
			return aSysCodeInfos;
		}
		List<Object> codeMaps = new ArrayList<Object>();
		Map<String,Object> codeMap;
		Map<String,Object> map = new HashMap<String,Object>();
		List<ASysCodeInfo> codeList;
		List<?> codeListLevel;
		for ( ASysCodeInfo aSysCodeInfo : aSysCodeInfos ) {
			
			map.put(TYPE_CODE_KEY, aSysCodeInfo.getValCode());
			codeList=aSysCodeInfoMapper.getList(map);
			
			codeMap = BeanUtil.transBean2Map(aSysCodeInfo);
			codeMap.put(LEVEL_CHILDREN_KEY, getLevelCode(codeList, level - 1 ));
			codeMaps.add(codeMap);
		}
		
		return codeMaps;
	}
	@Override
	public void setBaseMapper() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public ASysCodeInfo getByPrimaryKey(Serializable primaryKey) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public ASysCodeInfo get(String propName, String value) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public List<ASysCodeInfo> getList(String propName, String value) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void updateByPrimaryKey(ASysCodeInfo t) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void deleteByPrimaryKey(Serializable primaryKey) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void deleteObj(List<ASysCodeInfo> list) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void save(ASysCodeInfo t) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void delete(List<String> ids) {
		// TODO Auto-generated method stub
		
	}
}

